/**  
* @Title: WechatBankCardController.java
* @Package com.hjb.service.controller.Wechat
* @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>panxinwen
* @date 2018年1月10日 下午11:41:20
* @version V1.0  
*/ 
package com.hjb.service.controller.Wechat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hjb.service.Wechat.WechatBankCardService;
import com.hjb.service.annotation.LogInfo;
import com.hjb.service.annotation.LogType;
import com.hjb.service.com.ComVerifyCodeService;
import com.hjb.service.domain.siteuseraccount.SiteUserBankcard;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
* @ClassName: WechatBankCardController
* @Description: TODO(公众号端租户银行卡信息维护)
* @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>panxinwen
* @date 2018年1月10日 下午11:41:20
*
*/
@RestController
@RequestMapping("/Wechat/bankCard")
@Api(value="/Wechat/bankCard",description="公众号端租户银行卡信息维护")
public class WechatBankCardController {
	
	@Autowired
	private WechatBankCardService wechatBankCardService;
	
	@Autowired
	private ComVerifyCodeService comVerifyCodeService;

	@SuppressWarnings("rawtypes")
	@LogInfo(logType = LogType.查询, operationContent = "获取短信验证码")
	@PostMapping("/getSmsCode")
	@ResponseBody
	@ApiOperation("获取短信验证码")
	@ApiImplicitParams({ 
		@ApiImplicitParam(paramType = "query", dataType = "String", name = "remainPhone", value = "预留手机号", required = true)
		})
	public ResponseEntity getSmsCode(
			@RequestParam(value = "remainPhone", required = true) String remainPhone){
		
		return ResponseEntity.ok(wechatBankCardService.getSmsCode(remainPhone));
		
	}
	
	@SuppressWarnings("rawtypes")
	@LogInfo(logType=LogType.修改,operationContent="租户绑定银行卡")
	@PostMapping("/supplementUserMsg")
	@ResponseBody
	@ApiOperation(value="租户绑定银行卡", notes="租户绑定银行卡")
	@ApiImplicitParams({ 
		@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "userId", value = "租户ID", required = true),
		@ApiImplicitParam(paramType = "query", dataType = "String", name = "cardName", value = "姓名", required = true),
		@ApiImplicitParam(paramType = "query", dataType = "String", name = "idcard", value = "身份证", required = true),
		@ApiImplicitParam(paramType = "query", dataType = "String", name = "cardNo", value = "银行卡号", required = true),
		@ApiImplicitParam(paramType = "query", dataType = "String", name = "smsCode", value = "短信验证码", required = true),
		@ApiImplicitParam(paramType = "query", dataType = "String", name = "remainPhone", value = "预留手机号", required = true)
		})
	public ResponseEntity bindingBankCard(
			@RequestParam(value = "userId", required = true) Integer userId,
			@RequestParam(value = "cardName", required = true) String cardName,
			@RequestParam(value = "idcard", required = true) String idcard,
			@RequestParam(value = "cardNo", required = true) String cardNo,
			@RequestParam(value = "smsCode", required = true) String smsCode,
			@RequestParam(value = "remainPhone", required = true) String remainPhone
			) {
		
		SiteUserBankcard siteUserBankcard = new SiteUserBankcard();
		siteUserBankcard.setCardName(cardName);
		siteUserBankcard.setUserId(userId);
		siteUserBankcard.setCardNo(cardNo);
		siteUserBankcard.setRemainPhone(remainPhone);
		siteUserBankcard.setType(2);
		siteUserBankcard.setStatus(1);
		siteUserBankcard.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		
		Map<String,String> map = new HashMap<String,String>();
		int re=0;
		try {
			re = comVerifyCodeService.verifyCode(remainPhone, smsCode, 2);
		} catch (ParseException e) {
			map.put("code", "404");
			map.put("msg", "服务器内部错误");
			e.printStackTrace();
			ResponseEntity.ok(map);
		}
		

		
		switch (re) {
		case 0:
			map.put("code", "401");
			map.put("msg", "验证码输入错误");
			break;
		case 1:
			map = wechatBankCardService.bindingBankCard(siteUserBankcard);
			break;
		case 2:
			map.put("code", "402");
			map.put("msg", "验证码已过期");
			break;
//			return ResponseEntity.ok(map);

		default:
			map.put("code", "403");
			map.put("msg", "位置错误");;
			break;
		}
		return ResponseEntity.ok(map);
		
//		if(smsCode.equals("8888")){
//			return ResponseEntity.ok(wechatBankCardService.bindingBankCard(siteUserBankcard));
//		}else{
//			Map<String,String> map = new HashMap<String,String>();
//			map.put("code", "401");
//			map.put("msg", "验证码输入错误");
//			return ResponseEntity.ok(map);
//		}
//		
		
	}
}
